package com.berje.vkalarm.vksdk.model;

import java.io.Serializable;
import org.json.JSONException;
import org.json.JSONObject;

import com.berje.vkalarm.vksdk.Api;

public class Audio implements Serializable {
	private static final long serialVersionUID = 1L;
	private long aid;
	private long owner_id;
	private String artist;
	private String title;
	private long duration;
	private String url;
	private Long lyrics_id;

	public static Audio parse(JSONObject o) throws NumberFormatException, JSONException {
		Audio ret = new Audio();
		ret.setAid(Long.parseLong(o.getString("aid")));
		ret.setOwner_id(Long.parseLong(o.getString("owner_id")));
		if (o.has("performer"))
			ret.setArtist(Api.unescape(o.optString("performer")));
		else if (o.has("artist"))
			ret.setArtist(Api.unescape(o.optString("artist")));
		ret.setTitle(Api.unescape(o.optString("title")));
		ret.setDuration(Long.parseLong(o.getString("duration")));
		ret.setUrl(o.optString("url", null));

		String tmp = o.optString("lyrics_id");
		if (tmp != null && !tmp.equals(""))//otherwise lyrics_id=null 
			ret.setLyrics_id(Long.parseLong(tmp));

		return ret;
	}

	public String toString() {
		return String.format("Audio{aid: %d, owner_id: %d, artist: %s, title: %s, duration: %d, url: %s, lyrics_id: %d}", aid, owner_id, artist, title, duration, url, lyrics_id);
	}

	public long getAid() {
		return aid;
	}

	public void setAid(long aid) {
		this.aid = aid;
	}

	public long getOwner_id() {
		return owner_id;
	}

	public void setOwner_id(long owner_id) {
		this.owner_id = owner_id;
	}

	public String getArtist() {
		return artist;
	}

	public void setArtist(String artist) {
		this.artist = artist;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public long getDuration() {
		return duration;
	}

	public void setDuration(long duration) {
		this.duration = duration;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public Long getLyrics_id() {
		return lyrics_id;
	}

	public void setLyrics_id(Long lyrics_id) {
		this.lyrics_id = lyrics_id;
	}
	
	public String getId() {
		return owner_id + "_" + aid;
	}

}